package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bo.ReasonEstimateBo;
import bo.StacTraceBo;
import vo.CauseVo;
import vo.StackTraceVo;
import dao.hibernate.CauseVoDAO;
import dao.hibernate.StackTraceVoDAO;
import dto.StackTraceDto;

import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StackTracePatternMtnServlet extends HttpServlet {
	private static Logger LOGGER = LoggerFactory.getLogger(StackTracePatternMtnServlet.class);
	
	/**
	 * Constructor of the object.
	 */
	public StackTracePatternMtnServlet() {
		super();
	}
	private StacTraceBo bo =new StacTraceBo();
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		LOGGER.debug("doGet"  );
		String keyCauseUid = request.getParameter("keyCauseUid");
		LOGGER.debug("keyCauseUid:  {}",keyCauseUid);
		/***
		 *Get  method 用於統計資料呈現
		 * ***/
		if(keyCauseUid != null){
			final	StackTraceDto dto = bo.getStackTracePattern(NumberUtils.toInt(keyCauseUid));
			request.setAttribute("stackTraceDto", dto);
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher("stackTraceMtn.jsp");
	    dispatcher.forward(request, response); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(final HttpServletRequest request, final HttpServletResponse response)
			throws ServletException, IOException {
		LOGGER.debug("doPost"  );
		/***
		 * Post  method 用於處理人為判定stuck thread 的CRUD
		 * ***/
		String stackTraceUid = request.getParameter("stackTraceUid");
		String otherText = request.getParameter("otherText");
		String method = request.getParameter("method");
		String causeUid = request.getParameter("causeUid");
		String originalCauseUid = request.getParameter("originalCauseUid");
		LOGGER.debug("stackTraceUid: {}",stackTraceUid);
		LOGGER.debug("method: {}",method);
		LOGGER.debug("originalCauseUid: {}",originalCauseUid); 
		LOGGER.debug("causeUid: {}",causeUid);
		LOGGER.debug("otherText:  {}",otherText);
		if("new".equalsIgnoreCase(method)){
			bo.insertNewCauseByStackTraceUid(otherText, Integer.valueOf(stackTraceUid));
		}else if("modify".equalsIgnoreCase(method)){
			bo.updateNewCauseByStackTraceUid(Integer.valueOf(causeUid), Integer.valueOf(stackTraceUid));			
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher("stackTraceMtn.jsp");
	    dispatcher.forward(request, response); 
	}

}
